import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:pornhub_app/model/original_flavor_cell_model/original_flavor_cell_model.dart';
import 'package:pornhub_app/repositories/original_flavor.dart';

class OriginalFlavorListController extends GetxController {
  final pagingController =
      PagingController<int, OriginalFlavorCellModel>(firstPageKey: 1);

  final refreshController = EasyRefreshController();

  Future<void> fetch({int page = 1}) async {
    final result = await OriginalFlavorRepositoryImpl().fetchOriginalFlavorList(
      page: page,
    );

    final isLastPage = result.length < 10;

    if (isLastPage == true) {
      pagingController.appendLastPage(result);
    } else {
      pagingController.appendPage(result, page + 1);
    }
  }

  void pagingCongrollerAddPageRequestListener(int pageKey) async {
    fetch(page: pageKey);
  }

  @override
  void onInit() {
    super.onInit();
    pagingController
        .addPageRequestListener(pagingCongrollerAddPageRequestListener);
  }

  @override
  void onClose() {
    refreshController.dispose();
    super.onClose();
  }
}
